package com.example.yucheng.controller;

import com.example.yucheng.domain.User;
import com.example.yucheng.service.LoginService;
import com.example.yucheng.util.JwtUtils;
import com.example.yucheng.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin
public class LoginController {

    @Autowired
    LoginService loginService;

    @PostMapping("login")
    public Result login(@RequestBody User admin){
        User login = loginService.Login(admin);
        if (login!=null){
            Map<String,String> tokenmap = new HashMap<>();
            tokenmap.put("usename",admin.getUserName());
            tokenmap.put("password",admin.getPassword());
            String token = JwtUtils.getToken(tokenmap);
            System.out.println("登录成功");
            return Result.success(token);
        }else{
            System.out.println("登录失败");
            return Result.error(400,"密码或账号错误");
        }

    }

}
